﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Things.IDAL;
using Things.DBUtility;//Please add references
namespace Things.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:GoodsComments
	/// </summary>
	public partial class GoodsComments:IGoodsComments
	{
        protected string sqlSelectMainComments = @"select top 20 T1.CommentTitle, t1.Comments, t3.GoodsName, t1.GoodsPriceID, t1.Rate
                                                    from Things_GoodsComments as T1
	                                                    join Things_GoodsPrice as T2 on T1.GoodsPriceID=t2.ID
	                                                    join Things_Goods as T3 on t2.GoodsID=t3.GoodsID
                                                    where T1.IsMain='true'";


		public GoodsComments()
		{}
		#region  Method

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(Guid CommentsID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Things_GoodsComments");
			strSql.Append(" where CommentsID=@CommentsID ");
			SqlParameter[] parameters = {
					new SqlParameter("@CommentsID", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = CommentsID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid GoodsPriceID, string buyerAccount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Things_GoodsComments");
            strSql.Append(" where GoodsPriceID=@GoodsPriceID and  BuyerAccount=@BuyerAccount");
            SqlParameter[] parameters = {
					new SqlParameter("@GoodsPriceID", SqlDbType.UniqueIdentifier,16),	
					new SqlParameter("@BuyerAccount", SqlDbType.NVarChar,100)		};
            parameters[0].Value = GoodsPriceID;
            parameters[1].Value = buyerAccount;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public int GetAllSuccessfulComments(Guid GoodsPriceID, string buyerAccount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Things_GoodsComments");
            strSql.Append(" where GoodsPriceID=@GoodsPriceID and  BuyerAccount=@BuyerAccount");
            SqlParameter[] parameters = {
					new SqlParameter("@GoodsPriceID", SqlDbType.UniqueIdentifier,16),	
					new SqlParameter("@BuyerAccount", SqlDbType.NVarChar,100)		};
            parameters[0].Value = GoodsPriceID;
            parameters[1].Value = buyerAccount;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            int result = 0;
            if (ds != null && ds.Tables.Count > 0)
            {
                result = ds.Tables[0].Rows.Count;
            }
            return result;
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Things.Model.GoodsComments model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Things_GoodsComments(");
            strSql.Append("CommentsID,GoodsPriceID,Comments,BuyerAccount,CommentDate,Rate,CommentTitle, IsMain)");
			strSql.Append(" values (");
            strSql.Append("@CommentsID,@GoodsPriceID,@Comments,@BuyerAccount,@CommentDate,@Rate,@CommentTitle, @IsMain)");
			SqlParameter[] parameters = {
					new SqlParameter("@CommentsID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@GoodsPriceID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@Comments", SqlDbType.NVarChar),
					new SqlParameter("@BuyerAccount", SqlDbType.NVarChar,100),
					new SqlParameter("@CommentDate", SqlDbType.DateTime),
					new SqlParameter("@Rate", SqlDbType.Float,8),
					new SqlParameter("@CommentTitle", SqlDbType.NVarChar,100),
					new SqlParameter("@IsMain", SqlDbType.Bit,1)};
			parameters[0].Value = Guid.NewGuid();
			parameters[1].Value = model.GoodsPriceID;
			parameters[2].Value = model.Comments;
			parameters[3].Value = model.BuyerAccount;
			parameters[4].Value = model.CommentDate;
			parameters[5].Value = model.Rate;
			parameters[6].Value = model.CommentTitle;
            parameters[7].Value = model.IsMain;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Things.Model.GoodsComments model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Things_GoodsComments set ");
			strSql.Append("GoodsPriceID=@GoodsPriceID,");
			strSql.Append("Comments=@Comments,");
			strSql.Append("BuyerAccount=@BuyerAccount,");
			strSql.Append("CommentDate=@CommentDate,");
			strSql.Append("Rate=@Rate,");
            strSql.Append("CommentTitle=@CommentTitle");
            strSql.Append("IsMain=@IsMain");
			strSql.Append(" where CommentsID=@CommentsID ");
			SqlParameter[] parameters = {
					new SqlParameter("@GoodsPriceID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@Comments", SqlDbType.NVarChar),
					new SqlParameter("@BuyerAccount", SqlDbType.NVarChar,100),
					new SqlParameter("@CommentDate", SqlDbType.DateTime),
					new SqlParameter("@Rate", SqlDbType.Float,8),
					new SqlParameter("@CommentTitle", SqlDbType.NVarChar,100),
					new SqlParameter("@IsMain", SqlDbType.Bit, 1),
					new SqlParameter("@CommentsID", SqlDbType.UniqueIdentifier,16)};
			parameters[0].Value = model.GoodsPriceID;
			parameters[1].Value = model.Comments;
			parameters[2].Value = model.BuyerAccount;
			parameters[3].Value = model.CommentDate;
			parameters[4].Value = model.Rate;
            parameters[5].Value = model.CommentTitle;
            parameters[6].Value = model.IsMain;
			parameters[7].Value = model.CommentsID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid CommentsID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Things_GoodsComments ");
			strSql.Append(" where CommentsID=@CommentsID ");
			SqlParameter[] parameters = {
					new SqlParameter("@CommentsID", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = CommentsID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string CommentsIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Things_GoodsComments ");
			strSql.Append(" where CommentsID in ("+CommentsIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Things.Model.GoodsComments GetModel(Guid CommentsID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 CommentsID,GoodsPriceID,Comments,BuyerAccount,CommentDate,Rate,CommentTitle from Things_GoodsComments ");
			strSql.Append(" where CommentsID=@CommentsID ");
			SqlParameter[] parameters = {
					new SqlParameter("@CommentsID", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = CommentsID;

			Things.Model.GoodsComments model=new Things.Model.GoodsComments();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["CommentsID"]!=null && ds.Tables[0].Rows[0]["CommentsID"].ToString()!="")
				{
					model.CommentsID= new Guid(ds.Tables[0].Rows[0]["CommentsID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["GoodsPriceID"]!=null && ds.Tables[0].Rows[0]["GoodsPriceID"].ToString()!="")
				{
					model.GoodsPriceID= new Guid(ds.Tables[0].Rows[0]["GoodsPriceID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Comments"]!=null && ds.Tables[0].Rows[0]["Comments"].ToString()!="")
				{
					model.Comments=ds.Tables[0].Rows[0]["Comments"].ToString();
				}
				if(ds.Tables[0].Rows[0]["BuyerAccount"]!=null && ds.Tables[0].Rows[0]["BuyerAccount"].ToString()!="")
				{
					model.BuyerAccount=ds.Tables[0].Rows[0]["BuyerAccount"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CommentDate"]!=null && ds.Tables[0].Rows[0]["CommentDate"].ToString()!="")
				{
					model.CommentDate=DateTime.Parse(ds.Tables[0].Rows[0]["CommentDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Rate"]!=null && ds.Tables[0].Rows[0]["Rate"].ToString()!="")
				{
					model.Rate=decimal.Parse(ds.Tables[0].Rows[0]["Rate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CommentTitle"]!=null && ds.Tables[0].Rows[0]["CommentTitle"].ToString()!="")
				{
					model.CommentTitle=ds.Tables[0].Rows[0]["CommentTitle"].ToString();
				}
                if (ds.Tables[0].Rows[0]["IsDeleted"] != null && ds.Tables[0].Rows[0]["IsDeleted"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsMain"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsMain"].ToString().ToLower() == "true"))
                    {
                        model.IsMain = true;
                    }
                    else
                    {
                        model.IsMain = false;
                    }
                }
				return model;
			}
			else
			{
				return null;
			}
		}

        /// <summary>
        /// 获得主要的评论
        /// </summary>
        public DataSet GetMainComments()
        {
            return DbHelperSQL.Query(sqlSelectMainComments);
        }

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM Things_GoodsComments ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM Things_GoodsComments ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Things_GoodsComments ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.CommentsID desc");
			}
			strSql.Append(")AS Row, T.*  from Things_GoodsComments T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Things_GoodsComments";
			parameters[1].Value = "CommentsID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

